package ch.ffhs.bscinf2009.organizeanything.gui;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import ch.ffhs.bscinf2009.organizeanything.Environment;
import ch.ffhs.bscinf2009.organizeanything.R;
import ch.ffhs.bscinf2009.organizeanything.data.AbstractType;
import ch.ffhs.bscinf2009.organizeanything.data.Organizable;

/**
 * @author lir
 * The organizable instance list shows all the object instances in a list view. For example Harry Potter 1 to 6 for books.
 */
public class OrganizableInstanceList extends Activity {
	private Organizable definition;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.organizable_instance_list);
		
		definition = (Organizable) getIntent().getExtras().getSerializable("definition");
		TextView textField = (TextView)findViewById(R.id.instace_list_title);
		textField.setText(definition.getOrganizableName());

		refresh();
	}

	/**
	 * Read all entries and compile a list for details
	 */
	private void refresh() {
		ListView listView = (ListView) findViewById(R.id.instance_list);
		
		final List<Organizable> orgs = Environment.getInstance().getDB(this).readAllEntries(definition);
			String[] values = new String[orgs.size()];
			
			int i = 0;
			for (Organizable organizable : orgs) {
				values[i] = ((AbstractType) organizable.getProperties().get(0)).getData();
				i++;
			}

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);

			// Assign adapter to ListView
			listView.setAdapter(adapter);
			
			listView.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
					final Intent i = new Intent(OrganizableInstanceList.this, OrganizableInstanceDetail.class);
					i.putExtra("detail", orgs.get(position));
					startActivity(i);
				}
			});
	}
	
	@Override
	protected void onResume() {
		refresh();
		super.onResume();
	}
	
	/**
	 * Creates a new organizable instance
	 * @param view the view which the method was called from
	 */
	public void createOrganizableInstance (View view){
		final Intent i = new Intent(this, CreateNewOrganizableInstance.class);
		i.putExtra("definition", definition);
		startActivity(i);
	}
	
	/**
	 * Asks the user if he wants to remove all instances and the definition optionally. Cancel possible.
	 * @param view
	 */
	public void removeAll (View view){
		DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
		    //@Override
		    public void onClick(DialogInterface dialog, int which) {
		        switch (which){
		        case DialogInterface.BUTTON_POSITIVE:
		            removeAllInstances(true);
		            break;

		        case DialogInterface.BUTTON_NEGATIVE:
		            removeAllInstances(false);
		            break;
		        }
		    }
		};
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Do you want to remove all objects and their definition, too?").setPositiveButton("Yes", dialogClickListener)
		    .setNegativeButton("No", dialogClickListener).setCancelable(true).show();
		
	
	}

	/**
	 * Removes all entries of a type, and optional the type also.
	 * @param removeDefinition if the definition should be removed as well
	 */
	protected void removeAllInstances(boolean removeDefinition) {
		Environment.getInstance().getDB(this).deleteEntry(Environment.getInstance().getDB(this).readAllEntries(definition));
		if(removeDefinition){
			Environment.getInstance().getDB(this).removeDefinition(definition);
			// go back to home screen
			final Intent i = new Intent(OrganizableInstanceList.this,OrganizableTypeList.class);
			startActivity(i);
		}else{
			refresh();
		}
	}

}
